﻿#include "stdafx.h"
#include "SceneTest.h"
#include "TextureDrawObject.h"

SceneTest::SceneTest()
{
}


SceneTest::~SceneTest()
{
}


bool SceneTest::Init()
{
	bool fok;

	auto wnd = MainWin::GetInstance();
	window_size_callback(wnd->GetWidth(), wnd->GetHeight());
	m_gameObjList1.SetRootMatrixPtr(&m_camera.GetViewMatrix());

	auto obj = new SkeletonAnimation;
	m_texMgr.SetLoadDir("./Data/Logo/");
	fok = m_model.Load("D:/game/mu/mu074/Data/Logo/Face03.bmd", &m_texMgr);
	obj->Init(&m_camera, &m_model, &Shaders::GetInstance()->m_sBoneModel);
	m_gameObjList1.Add(obj);

	//m_skelModel.SetLocalMatrixPtr(&m_camera.GetViewMatrix());
	//m_gameObjects[0].push_back(texDraw);
	//auto trans = texDraw->m_transformObject.Add();
	//transRoot1->AddChild((Transform*)trans);
	//BoundBoxData bdBox;
	//VertexAttribPointerParam params[3];
	//params[0].Set(0, 3, GL_FLOAT, GL_FALSE, sizeof(BoundBoxData), (void*)offsetof(BoundBoxData, min));
	//params[1].Set(1, 3, GL_FLOAT, GL_FALSE, sizeof(BoundBoxData), (void*)offsetof(BoundBoxData, max));
	//params[2].Set(2, 3, GL_FLOAT, GL_FALSE, sizeof(BoundBoxData), (void*)offsetof(BoundBoxData, rgb));
	//bdBox.min = glm::make_vec3(m_model.m_boundingBoxMin);
	//bdBox.max = glm::make_vec3(m_model.m_boundingBoxMax);
	//bdBox.rgb = glm::vec3(1, 0, 0);
	//m_vaoMesh.GenVaoVbo(&bdBox, 1, sizeof(BoundBoxData), params, 3, 0, 0, 0);
	//VaoMesh::SubMesh subMesh;
	//subMesh.mode = GL_POINTS;
	//subMesh.vertElementLocation = 0;
	//subMesh.vertElementCount = 1;
	//m_vaoMesh.m_subMeshs.push_back(subMesh);
	//Material mate;
	//std::vector<Material> vmate(1);
	//vmate.push_back(mate);
	//m_renderObj.Init(&m_camera, &m_vaoMesh, vmate, &Shaders::GetInstance()->m_sBoundBox);
	//m_renderObj.SetLocalMatrixPtr(&m_camera.GetViewMatrix());

	//for (auto &p : g_DrawBoundBoxParam)
	//{
	//	p.bdMin = glm::vec4(glm::make_vec3(m_model.m_boundingBoxMin),1);
	//	p.bdMax = glm::vec4(glm::make_vec3(m_model.m_boundingBoxMax),1);
	//}
	//g_DrawBoundBoxParam.bdMin
	//g_DrawBoundBoxParam.bdMax
	//g_DrawBoundBoxParam.color = glm::vec4(1, 0, 0, 1);
	g_centerPoint = glm::vec3(0.0f, 0, -100);
	g_scale = 0.2;
	return true;
}

void SceneTest::Update()
{
	auto obj = m_gameObjList1.GetGameObject(0);
	glm::mat4 t = glm::scale(glm::rotate<float>(glm::translate<float>(glm::mat4(1.0f), g_centerPoint), glm::radians(-90.0f), glm::vec3(1, 0, 0)), glm::vec3(g_scale));;
	obj->SetLocalMatrix(t);
	m_gameObjList1.Update();
	//auto s = &Shaders::GetInstance()->m_sBallOfFire;
	//auto i = glGetUniformLocation(s->m_program, "iGlobalTime");
	//static float t;
	//s->UseProgram();
	//t += FrameTime::GetInstance()->GetDeltaTime()*10;
	//glUniform1f(i, t);
	//m_viewport1.Use();
	//m_camera.SetViewMatrix(glm::translate<float>(g_centerPoint) * glm::rotate<float>(g_scale,0,1,0) * glm::rotate<float>(-90,1,0,0));
	//m_skelModel.Update();
	//m_skelModel.UpdateWorldMatrix();
	//m_skelModel.Draw();

	//m_renderObj.UpdateWorldMatrix();
	//m_renderObj.Draw();
	//return;
	//UniformBufferObject &ubo = Shaders::GetInstance()->m_uboDrawInst;
	//static float rx, ry, rz;
	//float f = FrameTime::GetInstance()->GetDeltaTime() * 10;
	//rx += f;
	//ry += f;
	//rz += f;
	//glm::mat4 m = glm::translate<float>(g_centerPoint) * glm::scale<float>(glm::vec3(g_scale)) * glm::rotate<float>(rx, 1, 0, 0) * glm::rotate<float>(rx, 0, 1, 0) * glm::rotate<float>(rx, 0, 0, 1) * glm::translate<float>(-150 * 5, -150 * 5, 250 * 2.5);
	//m_camera.SetViewMatrix(m);
	////ubo.Bind();
	//for (int z = 0; z < 5; z++)
	//{
	//	for (int y = 0; y < 10; y++)
	//	{
	//		for (int x = 0; x < 10; x++)
	//		{
	//			g_DrawBoundBoxParam[x + y * 10 + z * 100].color = glm::vec4((x + 1) / 10.0, (y + 1) / 10.0, (z + 1) / 10.0, 1);
	//			g_DrawBoundBoxParam[x + y * 10 + z * 100].Mvp = m_camera.GetViewProjectionMatrix() * glm::translate<float>(150 * x, 150 * y, -250 * z);
	//		}
	//	}
	//}
	//	
	//ubo.SetBufferData(0, sizeof(DrawBoundBoxParam) * 500, g_DrawBoundBoxParam, true);		
	////ubo.UnBind();

	//Shaders::GetInstance()->m_sBoundBox.UseProgram();

	//glBindVertexArray(Shaders::GetInstance()->m_vaoNoVert);

	//int indices[] = { 0 };
	////for (int i = 0; i < 1; i++)
	////{
	//	//glDrawArrays(GL_POINTS, 0, 1);
	//	glDrawArraysInstancedBaseInstance(GL_POINTS, 0, 1, 3, 2);

	//	//glDrawElementsInstancedBaseInstance(GL_POINTS, 1, GL_UNSIGNED_INT, indices, 3, i);
	////}
	////glDrawArraysInstanced(GL_POINTS, 0, 1, 500);
	//glBindVertexArray(0);
	//Shaders::GetInstance()->m_sBoundBox.UseProgram0();
	return;

	glLoadIdentity();
	float mat[4][4];
	//glRotatef(-45, 0, 1, 0);
	//auto mat1 = glm::scale<float>(glm::vec3(.5f));
	//glScalef(0.5f, 0.5f, 0.5f);

	glColor3f(1, 1, 1);
	float verts[] = { 0.f, 0.5f, 1.f,
		0.f, 0.f, 1.f,
		0.5f, 0.f, 1.f,
		0.5f, 0.5f, 1.f };
	float colors[] = { 1.f, 0.f, 0.f,
		0.f, 1.f, 0.f,
		0.f, 0.f, 1.f,
		1.f, 1.f, 0.f };

	glEnable(GL_DEPTH_TEST);
	glTranslatef(0, 0, -2.5);
	glPushMatrix();
	//gluLookAt(0, 0, 0, 0, 0, -116.9, 0, 1, 0);
	glTranslatef(0.55f, 0.25f, 0.f);
	static int r = 20;
	r++;
	glGetFloatv(GL_MODELVIEW_MATRIX, mat[0]);
	glRotatef(r, 0, 0, 1);
	//glTranslatef(-0.25, -0.25, 0);
	glBegin(GL_TRIANGLE_FAN);
	for (int i = 0; i < 4; i++)
	{
		auto m4 = glm::make_mat4((float*)mat);
		auto m3 = glm::mat3(m4);
		auto v3 = glm::make_vec3((float*)verts + i * 3);
		v3 = v3 * m3;
		glColor3fv(colors + i * 3);
		//glVertex3fv(verts + i * 3);
		glVertex3fv(&v3.x);
	}
	glEnd();
	glPopMatrix();

	glBegin(GL_LINES);
	glColor3f(1, 1, 1);
	glVertex3f(0, 0, 0);
	glVertex3f(1000, 0, 0);
	glVertex3f(0, 0, 0);
	glVertex3f(0, 1000, 0);

	////for (int i = 0; i < 10; i++)
	////{
	////	glVertex3f(0, 0, i*10);
	////	glVertex3f(1000, 0, i*10);
	////}

	//glColor3f(0, 1, 0);
	//glVertex3f(0, 0, 0);
	//glVertex3f(0, 1000, 0);

	//glColor3f(0, 0, 1);
	//glVertex3f(0, 0, 0);
	//glVertex3f(0, 0, 100);
	glEnd();
}

void SceneTest::window_size_callback(int width, int height)
{
	m_viewport1.SetSize(width, height);
	m_viewport1.Use();
	m_camera.Perspective(45, m_viewport1.m_width / (float)m_viewport1.m_height, 0.1, 2800);
	//auto w2 = m_viewport1.m_width / 2;
	//auto h2 = m_viewport1.m_height / 2;
	//m_camera.Ortho(-w2, w2, -h2, h2, 100, -100);
}